package com.heyue.demo.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fenghe
 * @date 2022/2/10 8:25
 */
public class AtomicDemo {
    private AtomicInteger val = new AtomicInteger();

    public static void main(String[] args) {
        AtomicDemo atomicDemo = new AtomicDemo();

        for (int i = 0; i < 10; ++i) {
            new Thread(atomicDemo::increase).start();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(atomicDemo.getVal().toString());
    }

    private void increase() {
        for (int i = 0; i < 1000; i++) {
            this.val.incrementAndGet();
        }
    }

    private AtomicInteger getVal() {
        return this.val;
    }
}
